package com.nxmu.hire.init;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.nxmu.hire.common.exception.SystemException;
import com.nxmu.hire.model.Research;
import com.nxmu.hire.model.enums.CollegeEnum;
import com.nxmu.hire.model.param.CollegeParam;
import com.nxmu.hire.model.param.ParameterFactory;

/**
 * 初始化学院和教研室
 */
@Component
public class CollegeInit implements ApplicationListener<ApplicationEvent> {

    private final static Logger LOGGER = LoggerFactory.getLogger(CollegeInit.class);

    private final static String SEPARATOR_COMMA = ",";

    private final static String COLLEGE_FILEPATH = "classpath:/data/college.csv";

    @Autowired
    private ParameterFactory parameterFactory;

    @Value(COLLEGE_FILEPATH)
    private Resource resource;

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        Map<CollegeEnum, CollegeParam> collegeEnumCollegeParamMap = resolveCsvData();
        parameterFactory.collegeEnumCollegeParamMap = collegeEnumCollegeParamMap;
    }

    public Map<CollegeEnum, CollegeParam> resolveCsvData(){
        Map<CollegeEnum, CollegeParam> colleges = new HashMap<>();
        List<String> lines = null;
        try {
            lines = IOUtils.readLines(resource.getInputStream(),"UTF-8");
        } catch (IOException e) {
            LOGGER.error("初始化College参数异常");
        }

        if (lines != null && !lines.isEmpty()) {
            for(String line : lines){
                final String[] lineArray = line.split(SEPARATOR_COMMA);
                if (lineArray.length < 3) throw new SystemException("初始化College参数异常 --> college.csv单元格不能为空！");
                for (String s : lineArray) {
                    if (StringUtils.isEmpty(s)) throw new SystemException("初始化College参数异常 --> college.csv单元格不能为空！");
                }

                CollegeEnum college;
                try {
                    college = CollegeEnum.valueOf(lineArray[0]);
                } catch (Exception e) {
                    throw new SystemException("初始化College参数异常 --> college.csv学院栏代码错误！错误代码 --> " + lineArray[0]);
                }

                if (colleges.containsKey(college)) {
                    colleges.get(college).researches.add(new Research(Integer.valueOf(lineArray[1]),lineArray[2]));
                } else {
                    CollegeParam collegeParam = new CollegeParam();
                    List<Research> researches = new ArrayList<>();
                    researches.add(new Research(Integer.valueOf(lineArray[1]),lineArray[2]));

                    collegeParam.college = college;
                    collegeParam.researches = researches;
                    colleges.put(college,collegeParam);
                }
            }
        }
        return colleges;
    }
}
